<?php 
/**
 * 分类控制器
 */
class CategoryController extends AuthController{
    /**
     * 
     */
    private $model;
	
	public function __auto(){
	    $this->model = K('Cate');
	}
	
	
    public function index(){
		$data = $this->model->all();
		
		//把无限级分类变成树状结构，并且有层次(把所有相关子类聚集到一起，添加特殊符号来表示层次)
		$data = Data::tree($data,'cname');
		$this->assign('data',$data);
        $this->display(); 
    }
	

	public function add(){
		if(IS_POST){
			if(!$this->model->addDate()){
				$this->error($this->model->error);
			}
			$this->success('添加成功', U('index'));
		}
        $this->display(); 
    }
	
	public function addSon(){
		//获得通过GET方式传过来的cid
		$cid = Q('get.cid',0,'intval');
		if(IS_POST){
			if(!$this->model->addDate()){
				$this->error($this->model->error);
			}
			$this->success('添加成功', U('index'));
		}

		//获得所属分类
		$cate = $this->model->where("cid={$cid}")->find();
		//这里是为了显示父级分类信息
		$this->assign('cate',$cate);
		//所属类型
		$attr = K('Type')->all();
		$this->assign('attr',$attr);
        $this->display(); 
    }
	
	/**
	 * 编辑
	 */
	public function editSon(){
		$cid = Q('get.cid',0,'intval');
		if(IS_POST){
			if(!$this->model->editDate($cid)){
				$this->error($this->model->error);
			}
			$this->success('修改成功', U('index'));
		}
		
		//获得旧数据
		$oldData = $this->model->where("cid={$cid}")->find();
		$this->assign('oldData',$oldData);
		
		//这里是为了显示父级分类信息
		$pid = $oldData['pid'];
		$farData = $this->model->where("cid={$pid}")->find();
		$this->assign('farData',$farData);
		
		//获得所有类型
		$attr = K('Type')->all();
		$this->assign('attr',$attr);
		
        $this->display(); 
    }
	
	
	
	/**
	 * 编辑
	 */
	public function edit(){
		$cid = Q('get.cid',0,'intval');
		if(IS_POST){
			if(!$this->model->editDate($cid)){
				$this->error($this->model->error);
			}
			$this->success('修改成功', U('index'));
		}
		
		//获得旧数据
		$oldData = $this->model->where("cid={$cid}")->find();
		$this->assign('oldData',$oldData);
		//获得所有类型
		$attr = K('Type')->all();
		$this->assign('attr',$attr);
		
        $this->display(); 
    }
	
	/**
	 * 删除
	 */
	public function del(){
	    $cid = Q('get.cid',0,'intval');
		//先判断是否有子分类
		$sonCate = $this->model->where("pid={$cid}")->find();
		if($sonCate){
			$this->error('请先删除子类');
		}
		//删除分类
		$this->model->where("cid={$cid}")->delete();
		//1.提示信息 2.跳转地址 3.跳转时间
		$this->success('删除成功',U('index'),1);	
	}
	
 }
 
 
 
 
 
 
 
 
 
 
 













 ?>